import axios from 'axios'
// import {Message} from 'element-ui';
// import router from "@/router";


// //请求拦截器
// axios.interceptors.request.use(config => {
//     if (window.sessionStorage.getItem("tokenStr")) {
//         config.headers['Authorization'] =
//             window.sessionStorage.getItem('tokenStr');
//     }
//     return config;
// }, error => {
//     console.log(error);
// })
//
//
// //响应拦截器
// axios.interceptors.response.use(success => {
//     // 业务逻辑
//     if (success.status && success.status == 200) {
//         if (success.data.code == 500 || success.data.code == 401 || success.data.code == 403) {
//             Message.error({message: success.data.message});
//             return;
//         }
//         if (success.data.message) {
//             Message.success({message: success.data.message});
//         }
//     }
//     return success.data;
//
// }, error => {
//     if (error.response.code = 504 || error.data.code == 404) {
//         Message.error({message: "服务器被吃了"});
//     } else if (error.data.code == 403) {
//         Message.error({message: "权限不足，请联系管理员"});
//         return;
//     } else if (error.data.code == 401) {
//         Message.error({message: "尚未登录，请登录"});
//         router.replace("/");
//     } else {
//         if (error.message.data.message) {
//             Message.error({message: error.message.data.message});
//         } else {
//             Message.error({message: "未知错误！"});
//         }
//     }
// })

let baseUrl = ''
export const postRequest = (url, params) => {
    return axios({
        method: "post",
        url: `${baseUrl}${url}`,
        data: params
    });
}
export const deleteRequest = (url, params) => {
    return axios({
        method: "delete",
        url: `${baseUrl}${url}`,
        data: params
    });
}
export const putRequest = (url, params) => {
    return axios({
        method: "put",
        url: `${baseUrl}${url}`,
        data: params
    });
}
export const getRequest = (url, params) => {
    return axios({
        method: "get",
        url: `${baseUrl}${url}`,
        data: params
    });
}
export const aa = 7
let base = '';

/*
*封装get方法
*/
export function get(url, params) {
    return new Promise((resolve, reject) => {
        axios.get(base + url, {
                params: params
            }
        ).then(response => {
            resolve(response.data)
        }).catch(err => {
            reject(err)
        })
    })
}

/*
*封装post方法
*/
export function post(url, data) {
    return new Promise((resolve, reject) => {
        axios.post(base + url, data
        ).then(response => {
            resolve(response.data)
        }).catch(err => {
            reject(err)
        })
    })
}


/**
 * put方法，对应put请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function put(url, params) {
    return new Promise((resolve, reject) => {
        axios.put(base + url, params)
            .then(response => {
                resolve(response.data);
                // Loading.service(true).close();
                //  Message({message: '请求成功', type: 'success'});
            })
            .catch(err => {
                reject(err)
            })
    });
}


/**
 * delete
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function deletefn(url, params) {
    return new Promise((resolve, reject) => {
        axios.delete(base + url, params)
            .then(response => {
                resolve(response.data);
                // Loading.service(true).close();
                //  Message({message: '请求成功', type: 'success'});
            })
            .catch(err => {
                reject(err)
            })
    });
}